package com.ruoyi.web.controller.manage;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.manage.domain.Order;
import com.ruoyi.manage.domain.request.OrderRequest;
import com.ruoyi.manage.service.IAppOrderService;
import com.ruoyi.manage.service.IAppUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @Author: wwy
 * @Date: 2021/1/7 8:44
 */
@Api("移动端创建工单")
@RestController
@RequestMapping("/api")
public class AppOrderController extends BaseController {
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IAppOrderService appOrderService;
    /**
     * 获取用户提交的订单
     * @param orderRequest
     * @return
     */
    @ApiOperation("获取工单信息列表")
    @PostMapping("/getOrderList")
    public TableDataInfo getOrderList(@RequestBody OrderRequest orderRequest, HttpServletRequest request){
        LoginUser loginUser = tokenService.getLoginUser(request);
        String openId = loginUser.getOpenId();
        startPage();
        List<Order> order = appOrderService.getOrder(orderRequest, openId);
        return getDataTable(order);
    }

    /**
     * 根据主键获取工单
     * @param orderRequest
     * @return
     */
    @ApiOperation("获取工单信息")
    @PostMapping("/getOrder")
    public AjaxResult getOrder(@RequestBody OrderRequest orderRequest){
        return appOrderService.getOrder(orderRequest);
    }

    /**
     * 添加
     * @return
     */
    @ApiOperation("提交工单信息")
    @PostMapping("/addOrder")
    public AjaxResult addOrder(@RequestBody Order order,HttpServletRequest request){
        LoginUser loginUser = tokenService.getLoginUser(request);
        String openId = loginUser.getOpenId();
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());
        return appOrderService.addOrder(order,openId);
    }

    /**
     * 获取最低图片个数
     * @param jurisdictionId
     * @return
     */
    @ApiOperation("获取最低图片个数")
    @GetMapping("/getImageCount/{jurisdictionId}")
    public AjaxResult getImageCount(@PathVariable("jurisdictionId") Long jurisdictionId){
        return appOrderService.getImageCount(jurisdictionId);
    }
}
